package com.gree.rpc.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.gree.commom.utils.JsonUtil;
import com.gree.rpc.entity.GoodsCategory;
import com.gree.rpc.mapper.GoodsCategoryMapper;
import com.gree.rpc.service.GoodsCategoryService;
import com.gree.rpc.vo.GoodsCategoryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service(interfaceClass = GoodsCategoryService.class,timeout = 120000)
@Component
public class GoodsCategoryServiceImpl implements GoodsCategoryService {

    @Autowired
    private GoodsCategoryMapper goodsCategoryMapper;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Value("${goods.category.list.key}")
    private String goodsCategoryListKey;

    /**
     * 商品分类-列表
     * @return
     */
    @Override
    public List<GoodsCategoryVo> selectCategoryListForView() throws JsonProcessingException {
        ValueOperations<String,String> valueOpreations = redisTemplate.opsForValue();
        //查询Redis缓存是否有数据，有数据直接返回，没有数据去数据库查询
        String gcvListJson = valueOpreations.get(goodsCategoryListKey);
        if (!StringUtils.isEmpty(gcvListJson)){
            return JsonUtil.jsonToList(gcvListJson,GoodsCategoryVo.class);
        }
        //查询顶级分类--查询条件parent_id = 0 and level = 1
        QueryWrapper<GoodsCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id",0);
        queryWrapper.eq("level",1);
        List<GoodsCategory> goodsCategoryList = goodsCategoryMapper.selectList(queryWrapper);
        List<GoodsCategoryVo> categoryVoList = new ArrayList<>();
        for (GoodsCategory goodsCategory : goodsCategoryList) {
            GoodsCategoryVo goodsCategoryVo = new GoodsCategoryVo();
            BeanUtils.copyProperties(goodsCategory,goodsCategoryVo);
            //查询二级分类
            QueryWrapper<GoodsCategory> queryWrapperTwo = new QueryWrapper<>();
            queryWrapperTwo.eq("parent_id",goodsCategory.getId());
            queryWrapperTwo.eq("level",2);
            List<GoodsCategory> goodsCategoryListTwo = goodsCategoryMapper.selectList(queryWrapperTwo);
            List<GoodsCategoryVo> categoryVoListTwo = new ArrayList<>();
            for (GoodsCategory goodsCategoryTwo : goodsCategoryListTwo) {
                GoodsCategoryVo goodsCategoryVoTwo = new GoodsCategoryVo();
                BeanUtils.copyProperties(goodsCategoryTwo,goodsCategoryVoTwo);
                //查询三级分类
                QueryWrapper<GoodsCategory> queryWrapperThird = new QueryWrapper<>();
                queryWrapperThird.eq("parent_id",goodsCategoryTwo.getId());
                queryWrapperThird.eq("level",3);
                List<GoodsCategory> goodsCategoryListThird = goodsCategoryMapper.selectList(queryWrapperThird);
                List<GoodsCategoryVo> categoryVoListThird = new ArrayList<>();
                for (GoodsCategory categoryThird : goodsCategoryListThird) {
                    GoodsCategoryVo goodsCategoryVoThird = new GoodsCategoryVo();
                    BeanUtils.copyProperties(categoryThird,goodsCategoryVoThird);
                    categoryVoListThird.add(goodsCategoryVoThird);
                }
                goodsCategoryVoTwo.setChildren(categoryVoListThird);
                categoryVoListTwo.add(goodsCategoryVoTwo);
            }
            goodsCategoryVo.setChildren(categoryVoListTwo);
            categoryVoList.add(goodsCategoryVo);
        }

        //========================JDK8新特性======================
//        List<GoodsCategory> goodsCategoryList = goodsCategoryMapper.selectAllInfo();
//        List<GoodsCategoryVo> goodsCategoryVoList = goodsCategoryList.stream().map(e -> {
//            GoodsCategoryVo goodsCategoryVo = new GoodsCategoryVo();
//            BeanUtils.copyProperties(e, goodsCategoryVo);
//            return goodsCategoryVo;
//        }).collect(Collectors.toList());
//        Map<Short, List<GoodsCategoryVo>> map = goodsCategoryVoList.stream().collect(Collectors.groupingBy(GoodsCategoryVo::getParentId));
//        goodsCategoryVoList.forEach(goodsCategoryVo -> goodsCategoryVo.setChildren(map.get(goodsCategoryVo.getId())));
//        List<GoodsCategoryVo> categoryVoList = goodsCategoryVoList.stream().filter(e -> 1 == e.getLevel()).collect(Collectors.toList());
        //放入Redis缓存
        valueOpreations.set(goodsCategoryListKey, JsonUtil.object2JsonStr(categoryVoList));
        return categoryVoList;
    }
}
